package com.xsl.stu;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.*;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricActivityInstanceQuery;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.List;

@Slf4j
public class ActivityDemo
{

    private String flowKey = "myProcess_1";

    @Test
    public void testDeployment(){
        // 1.创建ProcessEngine
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        // 2.获取RepositoryService
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        // 3.使用service进行流程的部署,定义一个流程名称，把bpmn和png部署到数据中
        Deployment deployment = repositoryService.createDeployment()
                .name("出差申请")
                .addClasspathResource("bpmn/evection.bpmn")
                .addClasspathResource("bpmn/evection.png")
                .deploy();
        log.info("流程部署id:{}，流程部署名称:{}",deployment.getId(),deployment.getName());
    }


    /**
     * 启动流程实例
     */
    @Test
    public void testStartProcess(){
        // 1.创建ProcessEngine
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        // 2.获取RunTimeService
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        // 3.根据流程定义的id 启动流程
        ProcessInstance instance = runtimeService.startProcessInstanceByKey(flowKey);
        // 4.输入内容
        log.info("流程id:{}，活动实例id:{}，当前活动的id:{}",instance.getProcessDefinitionId(),instance.getId(),instance.getActivityId());
    }


    /**
     * 查询流程定义
     */
    @Test
    public void queryProcessDefinition(){
        //        获取引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        repositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();
//        得到ProcessDefinitionQuery 对象
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
//          查询出当前所有的流程定义
//          条件：processDefinitionKey =evection
//          orderByProcessDefinitionVersion 按照版本排序
//        desc倒叙
//        list 返回集合
        List<ProcessDefinition> definitionList = processDefinitionQuery.processDefinitionKey(flowKey)
                .orderByProcessDefinitionVersion()
                .desc()
                .list();
//      输出流程定义信息
        for (ProcessDefinition processDefinition : definitionList) {
            System.out.println("流程定义 id="+processDefinition.getId());
            System.out.println("流程定义 name="+processDefinition.getName());
            System.out.println("流程定义 key="+processDefinition.getKey());
            System.out.println("流程定义 Version="+processDefinition.getVersion());
            System.out.println("流程部署 ID ="+processDefinition.getDeploymentId());
        }




    }


    /**
     * 查询当前个人待执行的任务
     */
    @Test
      public void testFindPersonalTaskList() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //        创建TaskService
        TaskService taskService = processEngine.getTaskService();
        //        根据流程key 和 任务负责人 查询任务
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey(flowKey) //流程Key
                .taskAssignee("zhangsan")//只查询该任务负责人的任务
                .list();
        for (Task task : list) {
            log.info("流程实例id:{} 任务id:{} 任务负责人:{} 任务名称:{}",
                    task.getProcessInstanceId(),
                    task.getId(),
                    task.getAssignee(),
                    task.getName()
            );
        }
    }


    // 完成任务
    @Test
    public void completTask(){
        //        获取引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //        获取taskService
        TaskService taskService = processEngine.getTaskService();

        //        根据流程key 和 任务的负责人 查询任务
        //        返回一个任务对象
        Task task = taskService.createTaskQuery()
                .processDefinitionKey(flowKey) //流程Key
                .taskAssignee("caixukun")  //要查询的负责人
                .singleResult();

        log.info("流程实例id:{} 任务id:{} 任务负责人:{} 任务名称:{}",
                task.getProcessInstanceId(),
                task.getId(),
                task.getAssignee(),
                task.getName()
        );
        //        完成任务,参数：任务id
        taskService.complete(task.getId());
    }

    /**
     * 查看历史信息
     */
    @Test
    public void findHistoryInfo(){
//      获取引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        获取HistoryService
        HistoryService historyService = processEngine.getHistoryService();
//        获取 actinst表的查询对象
        HistoricActivityInstanceQuery instanceQuery = historyService.createHistoricActivityInstanceQuery();
//        查询 actinst表，条件：根据 InstanceId 查询
//        instanceQuery.processInstanceId("2501");
//        查询 actinst表，条件：根据 DefinitionId 查询
        instanceQuery.processDefinitionId("myEvection:1:4");
//        增加排序操作,orderByHistoricActivityInstanceStartTime 根据开始时间排序 asc 升序
        instanceQuery.orderByHistoricActivityInstanceStartTime().asc();
//        查询所有内容
        List<HistoricActivityInstance> activityInstanceList = instanceQuery.list();
//        输出
        for (HistoricActivityInstance hi : activityInstanceList) {
            System.out.println(hi.getActivityId());
            System.out.println(hi.getActivityName());
            System.out.println(hi.getProcessDefinitionId());
            System.out.println(hi.getProcessInstanceId());
            System.out.println("<==========================>");
        }
    }

}
